package com.tanhua.server.service;

import com.tanhua.domain.db.*;
import com.tanhua.domain.vo.PageResult;
import com.tanhua.domain.vo.SettingsVo;
import com.tanhua.domain.vo.UserInfoVoAge;
import com.tanhua.dubbo.api.BlackListApi;
import com.tanhua.dubbo.api.QuestionApi;
import com.tanhua.dubbo.api.SettingsApi;
import com.tanhua.dubbo.api.UserInfoApi;
import com.tanhua.server.interceptors.UserHolder;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collector;
import java.util.stream.Collectors;

@Service
public class SettingsService {

    @Reference
    private QuestionApi questionApi;

    @Reference
    private SettingsApi settingsApi;

    @Reference
    private BlackListApi blackListApi;

    @Reference
    private UserInfoApi userInfoApi;

    /**
     * 通用设置查询
     * @return
     */
    public SettingsVo querySettings() {
        //1. 获取登录用户信息(id,mobile)
        User loginUser = UserHolder.getUser();
        //2. 通过登录用户id调用api查询陌生人问题
        Question question = questionApi.findByUserId(loginUser.getId());
        //3. 通过登录用户id调用api查询通知设置
        Settings settings = settingsApi.findByUserId(loginUser.getId());
        //4. 构建返回的vo
        SettingsVo vo = new SettingsVo();
        //5. 设置vo中的每个属性
        vo.setPhone(loginUser.getMobile());
        vo.setStrangerQuestion(null!=question?question.getTxt():"你喜欢我吗?");
        if(null != settings){
            BeanUtils.copyProperties(settings, vo);
        }
        //6. 返回
        return vo;
    }

    /**
     * 通知 设置
     * @param vo
     */
    public void saveSettings(SettingsVo vo) {
        //1. 转成pojo
        Settings pojo = new Settings();
        BeanUtils.copyProperties(vo,pojo);
        //1.1 设置登录用户的id
        pojo.setUserId(UserHolder.getUserId());
        //2. 调用api保存
        settingsApi.save(pojo);
    }

    /**
     * 设置 陌生人问题
     * @param paramMap
     */
    public void saveQuestions(Map<String, String> paramMap) {
        //1. 构建pojo
        Question pojo = new Question();
        pojo.setTxt(paramMap.get("content"));
        //1.1 设置登录用户的id
        pojo.setUserId(UserHolder.getUserId());
        //2. 调用api保存
        questionApi.save(pojo);
    }

    /**
     * 黑名单列表
     * @param page
     * @param pageSize
     * @return

    public PageResult<UserInfoVoAge> blackListPage(Long page, Long pageSize) {
        //1. 获取登录用户id
        Long loginUserId = UserHolder.getUserId();
        //2. 通过登录用户id分页查询他的黑名单列表
        PageResult pageResult = blackListApi.findPageByUserId(loginUserId, page, pageSize);
        //3. 取出分页结果集
        List<BlackList> blackListList = (List<BlackList>) pageResult.getItems();
        List<UserInfoVoAge> voList = new ArrayList<>();
        //4. 非空判断
        if(!CollectionUtils.isEmpty(blackListList)) {
            //5. 遍历黑名单结果集
            for (BlackList blackList : blackListList) {
                //6. 通过用户id查询黑名单人信息
                UserInfo blackUserInfo = userInfoApi.findById(blackList.getBlackUserId());
                //7. 把pojo转成vo，
                UserInfoVoAge vo = new UserInfoVoAge();
                BeanUtils.copyProperties(blackUserInfo, vo);
                //添加到list
                voList.add(vo);
            }
        }
        //8. 设置到pageResult里, 替换掉pageResult原来的List<BlackList>, 替换成vo
        pageResult.setItems(voList);
        //9. 返回
        return pageResult;
    }*/

    /**
     * 黑名单列表  优化
     * @param page
     * @param pageSize
     * @return
     */
    public PageResult<UserInfoVoAge> blackListPage(Long page, Long pageSize) {
        //1. 获取登录用户id
        Long loginUserId = UserHolder.getUserId();
        //2. 通过登录用户id分页查询他的黑名单列表
        PageResult pageResult = blackListApi.findPageByUserId(loginUserId, page, pageSize);
        //3. 取出分页结果集
        List<BlackList> blackListList = (List<BlackList>) pageResult.getItems();
        List<UserInfoVoAge> voList = new ArrayList<>();
        //4. 非空判断
        if(!CollectionUtils.isEmpty(blackListList)) {
            // 取出所有黑名单人的id, 遍历list集合的每个元素,调用getBlackUserId方法, 收集到一个list里
            List<Long> blackUserIds = blackListList.stream().map(BlackList::getBlackUserId).collect(Collectors.toList());
            //List<Long> blackUserIds = new ArrayList<>();
            //for (BlackList blackList : blackListList) {
            //    // 遍历list集合的每个元素,调用getBlackUserId方法, 收集到一个list里
            //    blackUserIds.add(blackList.getBlackUserId());
            //}

            // 调用userInfoApi批量查询, 顺序乱了
            List<UserInfo> userInfoList = userInfoApi.findByBatchId(blackUserIds);

            // key=用户id, value=userInfo, u代表着userInfo对象
            Map<Long,UserInfo> userInfoMap = userInfoList.stream().collect(Collectors.toMap(UserInfo::getId, u->u));
            //for (UserInfo userInfo : userInfoList) {
            //    userInfoMap.put(userInfo.getId(), userInfo);
            //}

            // 转成voList
            // blackListList还保留着原有顺序
            for (BlackList blackList : blackListList) {
                // 黑名单人的id
                Long blackUserId = blackList.getBlackUserId();
                // 通过黑名单人的id 获取黑名单人的信息
                UserInfo userInfo = userInfoMap.get(blackUserId);
                // 转成vo
                UserInfoVoAge vo = new UserInfoVoAge();
                BeanUtils.copyProperties(userInfo, vo);
                voList.add(vo);
            }
        }
        //8. 设置到pageResult里, 替换掉pageResult原来的List<BlackList>, 替换成vo
        pageResult.setItems(voList);
        //9. 返回
        return pageResult;
    }

    /**
     * 移除黑名单
     * @param blackUserId
     */
    public void removeBlackList(Long blackUserId) {
        //1. 获取登录用户id
        Long loginUserId = UserHolder.getUserId();
        //2. 调用api移除
        blackListApi.remove(loginUserId, blackUserId);
    }
}
